define('filter', ['require'], function(require) {
  return function(Vue) {
    Vue.filter('plus', function(val1, val2){
      return val1 + val2;
    });
    Vue.filter('searchText', function(val, text){
      text = text.trim();
      text = text.split(' ');
      text.forEach(function(str){
        var reg = new RegExp(str, 'g');
        val = val.replace(reg, '<span>'+str+'</span>');
      });
      return val;
    });
    /**
     * 时间转年月份,su:yyyy年MM月dd日 hh:mm
     */
    Vue.filter('dateToString', function(value, format) {
      var time = new Date(value);
      var o = {
        "M+": time.getMonth() + 1, //月份
        "d+": time.getDate(), //日
        "h+": time.getHours(), //小时
        "m+": time.getMinutes(), //分
        "s+": time.getSeconds(), //秒
        "q+": Math.floor((time.getMonth() + 3) / 3), //季度
        "S": time.getMilliseconds() //毫秒
      };
      if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (time.getFullYear() + "").substr(4 - RegExp.$1.length));
      }
      for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
          format = format.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
      }
      return format;
    });

    /**
     * 至今的时间，
     */
    Vue.filter('toThisDay', function(value, format) {
      var time = Math.floor((Date.now() - (new Date(value)).getTime()) / 1000);
      if (time < 0) {
        return;
      }
      var y, M, d, h, m, s;

      s = time % 60;
      m = Math.floor(time / 60) % 60;
      h = Math.floor(time / 3600) % 24;
      d = Math.floor(time / 86400) % 30;
      M = Math.floor(time / 2592000) % 12;
      y = Math.floor(time / 31104000);
      var ret = '';
      switch (format) {
        case 's':
          ret = s + '秒' + ret;
          if (time < 60) {
            break;
          }
        case 'm':
          ret = m + '分钟' + ret;
          if (time < 3600) {
            if (m === 0) {
              return '刚刚';
            }
            break;
          }
        case 'h':
          ret = h + '小时' + ret;
          if (time < 86400) {
            if (h === 0) {
              return '刚才';
            }
            break;
          }
        case 'd':
          ret = d + '天' + ret;
          if (time < 2592000) {
            if (d === 0) {
              return '今天';
            }
            break;
          }
        case 'M':
          ret = M + '个月' + ret;
          if (time < 31104000) {
            if (M === 0) {
              return '这个月';
            }
            break;
          }
        case 'y':
          ret = y + '年' + ret;
          if (y === 0) {
            return '今年';
          }
      }
      return ret + '前';
    });
  };
});